

package com.walk.zworld.square.commend

import androidx.lifecycle.LiveData
import com.walk.zworld.square.model.HomePageRecommend
import com.walk.zworld.square.network.EyepetizerNetwork
import com.walk.zworld.square.network.api.MainPageService
import com.base.lib.base.mvvm.viewmodel.BaseRefreshViewModel

class HomeCommendViewModel : BaseRefreshViewModel<HomePageRecommend.Item>() {

    val network = EyepetizerNetwork.getInstance()
    var pageUrl = MainPageService.HOMEPAGE_RECOMMEND_URL
    var nextPageUrl: String? = null

    private fun netListData(): LiveData<List<HomePageRecommend.Item>> {
        launchUI {
            val repoResponse = network.mainPageService.getHomePageRecommend(pageUrl)
            nextPageUrl = repoResponse.nextPageUrl
            mData.value = repoResponse.itemList
        }
        return mData
    }

    override fun refreshData(params: Any?) {
        page = 0;
        pageUrl = MainPageService.HOMEPAGE_RECOMMEND_URL
        netListData();
    }

    override fun loadMore(params: Any?) {
        if (nextPageUrl != null){
            page += 1;
            pageUrl = nextPageUrl as String;
            netListData();
        }
    }
}